//
//  AutoScrollView.m
//  UI_scrollView的封装
//
//  Created by 梁新昌 on 16/3/6.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "AutoScrollView.h"
#import "UIImageView+WebCache.h"

@interface AutoScrollView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger currentIndex;
@end
@implementation AutoScrollView
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self pageControllBuild];
        [self timerAction];

        
    }
    return self;
}

//- (void)setPicMutableArray:(NSMutableArray<NSString *> *)picMutableArray {
//    _picMutableArray = picMutableArray;
//
//    
//    
//
//循环附图片
//    for (int i = 0; i < self.picMutableArray.count + 2; i++) {
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
//        
//        imageView.userInteractionEnabled = YES;
//        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTap:)]];
//        
//        if (i == 0) {
//            [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[self.picMutableArray.count - 1]]];
//        } else if (i == self.picMutableArray.count + 1) {
//            [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[0]]];
//        } else {
//            [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[i - 1]]];
//        }
//        [self.scrollView addSubview:imageView];
//        
//    }
//}
#pragma mark - 创建 scrollView
- (void)scrollViewBuild {
    UIScrollView *sc = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:sc];
    
    self.scrollView = sc;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * (self.picMutableArray.count + 2), 0);
//      循环附图片
    for (int i = 0; i < self.picMutableArray.count + 2; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTap:)]];
        
        if (i == 0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[self.picMutableArray.count - 1]]];
        } else if (i == self.picMutableArray.count + 1) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[0]]];
        } else {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.picMutableArray[i - 1]]];
        }
        [self.scrollView addSubview:imageView];
        
    }
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
}
/**点击图片将会被 block*/
- (void)imageViewDidTap:(UITapGestureRecognizer *)tap {
    
    self.block(self.scrollView.contentOffset.x / self.scrollView.frame.size.width);

    
}
- (void)scrollViewAction {
    if (self.scrollView.contentOffset.x / self.scrollView.frame.size.width == (self.picMutableArray.count)) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    } else {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width, 0);
    }
    
    if (self.scrollView.contentOffset.x / self.scrollView.frame.size.width == self.picMutableArray.count) {
        self.pageControl.currentPage = (self.picMutableArray.count - 1);
        _currentIndex = self.pageControl.currentPage;
    } else {
        
        self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
        _currentIndex = self.pageControl.currentPage;
    }
    
}
#pragma mark - 创建 pageControl
- (void)pageControllBuild {
    UIPageControl *pc = [[UIPageControl alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * 2 / 5, self.scrollView.frame.size.height - 20, self.scrollView.frame.size.width / 5, 10)];
    [self addSubview:pc];
    _pageControl = pc;
    self.pageControl.numberOfPages = self.picMutableArray.count;
    [self.pageControl addTarget:self action:@selector(pagecontrolAction:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark - NSTimer
- (void)timerAction {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollViewAction) userInfo:nil repeats:YES];
}

- (void)removeTimer {
    [self.timer invalidate];
}
- (void)dealloc {
    [self removeTimer];
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x / scrollView.frame.size.width == (self.picMutableArray.count + 1)) {
        [scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
        _pageControl.currentPage = 0;
        
    } else if (scrollView.contentOffset.x / scrollView.frame.size.width == 0) {
        [scrollView setContentOffset:CGPointMake(self.picMutableArray.count * self.scrollView.frame.size.width, 0) animated:NO];
        _pageControl.currentPage = self.picMutableArray.count;
    } else {
        _pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self timerAction];
}
#pragma mark - pageControl 事件
- (void)pagecontrolAction:(UIPageControl *)pageControl {
//    pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
}

@end
